using System.Reflection;
using Windows.Win32;
using Windows.Win32.Foundation;
using Microsoft.Web.WebView2.Core;
using NetScript2.tools;

namespace NetScript2;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // this.view.Source = new Uri("http://www.baidu.com");
        this.Load += new EventHandler(this.Form1_Load);
    }
    
    public  void testBtnClick(object sender, EventArgs e)
    {
        // WinTools.printProcessNames();
        HWND window = PInvoke.FindWindow(null,"excel_export – ApiController.java Administrator");
        RECT rect = new RECT();
        PInvoke.GetWindowRect(window, out rect);
        Console.WriteLine($"left:{rect.left};top:{rect.top};宽：{rect.Width};height:{rect.Height}");
        WinTools.CaptureWindow(window,1528, 920);
        // this.msgLabel.Text = "截图成功！";
    }
    //窗体装载事件
    private void Form1_Load(object sender, EventArgs e)
    {
        Console.WriteLine("执行了");
        this.view.CoreWebView2InitializationCompleted += new EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs>(this.WebView2InitializationCompleted);
        this.view.EnsureCoreWebView2Async();

    }
//webview2装载完成监听
    private void WebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
//本地路径
        string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        Console.WriteLine("currentPath:"+ currentPath);
//本地html文件，可以把css和js文件导入
        string filepath = currentPath + @"\\html\\index.html";
        if (File.Exists(filepath))
        {  //创建实例
            this.view.Source = new Uri(filepath);
            //导航完成监听
            view.NavigationCompleted += WebView_Navigated;
 
        }
    }
//导航完成后，可以写调用网页的js函数事件了。
    private async void WebView_Navigated(object? sender, CoreWebView2NavigationCompletedEventArgs e)
    {
 
        string str = "这里面是json格式的文本";
        //这里的task是网页js函数的返回值
        string task = await view.CoreWebView2.ExecuteScriptAsync($"loadBarChart('{str}')");
    }

}